package com.jflm.controller.api;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jflm.base.BaseAction;
import com.jflm.domain.Allocation;
import com.jflm.domain.Constant;
import com.jflm.domain.Consume;
import com.jflm.domain.RuleSet;
import com.jflm.domain.Shop;
import com.jflm.domain.ShopRule;
import com.jflm.domain.ShopService;
import com.jflm.mapper.RuleSetMapper;
import com.jflm.mapper.ShopMapper;
import com.jflm.mapper.ShopServiceMapper;
import com.jflm.service.IShopService;

/**
 * ****************** 类说明 *********************
 * 
 * @class : RedpacketInfoAction
 * @author : xusai
 * @version : 1.0
 * @description : 商户商家接口服务
 * @see : *********************************************
 */
@RestController
@RequestMapping("/api/shop")
public class ShopAction extends BaseAction {
	private static final Logger LOG = LoggerFactory.getLogger(ShopAction.class);
	@Autowired
	private IShopService shopService;

	@Autowired
	private ShopMapper shopMapper;

	@Autowired
	private ShopServiceMapper shopServiceMapper;

	@Autowired
	private RuleSetMapper ruleSetMapper;

	// 分页查询全部商户
	@PostMapping(value = "/findList")
	public Map<String, Object> findList(@RequestBody Shop shop) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		Page<Shop> page = null;
		page = PageHelper.startPage(shop.getPage(), shop.getRows());
		List<Shop> shopList = shopService.findList(shop);
		map.put(Constant.PAGE_ROWS, shopList);
		map.put(Constant.PAGE_TOTAL, page.getTotal());
		return map;
	}

	// 新增商户
	@PostMapping(value = "/add")
	public Shop add(@RequestBody Shop shop) throws Exception {
		String uuid = UUID.randomUUID().toString().replaceAll("-", "");
		Date date = new Date();
		Timestamp nowdate = new Timestamp(date.getTime());
		shop.setId(uuid);
		shop.setSetId(shop.getRuleSetId());
		shop.setBalance(shop.getMoney());
		shop.setUsedMoney(new BigDecimal(0));
		shop.setStatus(shop.getStatus());
		shop.setDel(0);
		shop.setPwd(com.jflm.util.MD5.md5(shop.getPwd()));
		shop.setCreateTime(nowdate);
		int addShop = shopService.add(shop);
		LOG.info("add" + addShop);
		return shop;
	}

	@RequestMapping(value = "/findOne", method = RequestMethod.POST)
	public List<Shop> findOne(@RequestBody Shop shop) throws Exception {
		List<Shop> shop2 = shopService.findOne(shop);
		LOG.info("#findOne() 响应参数：{}", gson.toJson(shop2));
		return shop2;
	}

	@PostMapping(value = "/updatePage")
	public int updatePage(@RequestBody Shop shop) throws Exception {
		int rul = shopService.update(shop);
		LOG.info("#updatePage() 响应参数：{}", gson.toJson(shop));
		return rul;
	}

	// 已开通服务查询
	@PostMapping(value = "/detailpages")
	public List<ShopService> detailpages(@RequestBody ShopService service) throws Exception {
		List<ShopService> shopServiceList = shopService.detailpages(service);
		RuleSet ruleSet = new RuleSet();
		ruleSet.setId(service.getSetId());
		ruleSet = ruleSetMapper.findOne(ruleSet);
		if (shopServiceList.size() > 0) {
			shopServiceList.get(0).setSetName(ruleSet.getName());
		}
		return shopServiceList;
	}

	// 规则集选择
	@PostMapping(value = "/find")
	public List<RuleSet> findSet() throws Exception {
		List<RuleSet> list = shopMapper.find();
		return list;
	}

	// 产品价格配置
	@PostMapping(value = "/addAllocation")
	public int addAllocation(@RequestBody Allocation allocation) throws Exception {
		String uuid = UUID.randomUUID().toString().replaceAll("-", "");
		Date date = new Date();
		Timestamp nowdate = new Timestamp(date.getTime());
		for (int i = 0; i < 7; i++) {
			if (i == 0) {
				allocation.setDel(0);
				allocation.setCreateTime(nowdate);
				allocation.setId(uuid + i);
				allocation.setPrice(allocation.getFqz());
				allocation.setType("0");
				allocation.setName("反欺诈");
				int flag = shopService.addAllocation(allocation);
				LOG.info("#addAllocation()产品价格配置" + flag);
			}
			if (i == 1) {
				Allocation allocationPfk = new Allocation();
				allocationPfk.setId(uuid + i);
				allocationPfk.setDel(0);
				allocationPfk.setCreateTime(nowdate);
				allocationPfk.setShopId(allocation.getShopId());
				allocationPfk.setPrice(allocation.getPfk());
				allocationPfk.setType("1");
				allocationPfk.setName("评分卡");
				int flag = shopService.addAllocation(allocationPfk);
				LOG.info("#addAllocation()产品价格配置" + flag);
			}
			if (i == 2) {
				Allocation allocationChsi = new Allocation();
				allocationChsi.setId(uuid + i);
				allocationChsi.setDel(0);
				allocationChsi.setCreateTime(nowdate);
				allocationChsi.setShopId(allocation.getShopId());
				allocationChsi.setPrice(allocation.getChsi());
				allocationChsi.setType("2");
				allocationChsi.setName("学信网");
				int flag = shopService.addAllocation(allocationChsi);
				LOG.info("#addAllocation()产品价格配置" + flag);
			}
			if (i == 3) {
				Allocation allocationGjj = new Allocation();
				allocationGjj.setId(uuid + i);
				allocationGjj.setDel(0);
				allocationGjj.setCreateTime(nowdate);
				allocationGjj.setShopId(allocation.getShopId());
				allocationGjj.setPrice(allocation.getGjj());
				allocationGjj.setType("3");
				allocationGjj.setName("公积金");
				int flag = shopService.addAllocation(allocationGjj);
				LOG.info("#addAllocation()产品价格配置" + flag);
			}
			if (i == 4) {
				Allocation allocationShe = new Allocation();
				allocationShe.setId(uuid + i);
				allocationShe.setDel(0);
				allocationShe.setCreateTime(nowdate);
				allocationShe.setShopId(allocation.getShopId());
				allocationShe.setPrice(allocation.getSheb());
				allocationShe.setType("4");
				allocationShe.setName("社保");
				int flag = shopService.addAllocation(allocationShe);
				LOG.info("#addAllocation()产品价格配置" + flag);
			}
			if (i == 5) {
				Allocation allocationYys = new Allocation();
				allocationYys.setId(uuid + i);
				allocationYys.setDel(0);
				allocationYys.setCreateTime(nowdate);
				allocationYys.setShopId(allocation.getShopId());
				allocationYys.setPrice(allocation.getYys());
				allocationYys.setType("5");
				allocationYys.setName("运营商");
				int flag = shopService.addAllocation(allocationYys);
				LOG.info("#addAllocation()产品价格配置" + flag);
			}
			if (i == 6) {
				Allocation allocationNut = new Allocation();
				allocationNut.setId(uuid + i);
				allocationNut.setDel(0);
				allocationNut.setCreateTime(nowdate);
				allocationNut.setShopId(allocation.getShopId());
				allocationNut.setPrice(allocation.getBank());
				allocationNut.setType("6");
				allocationNut.setName("银行卡");
				int flag = shopService.addAllocation(allocationNut);
				LOG.info("#addAllocation()产品价格配置" + flag);
			}
		}
		return 1;
	}

	// 消费查询
	@PostMapping(value = "/consumeDetail")
	public List<Consume> consumeDetail(@RequestBody Consume consume) throws Exception {
		List<Consume> consumeList = shopService.consumeDetail(consume);
		return consumeList;
	}

	// 查看规则集
	@PostMapping(value = "/findRuleList")
	public List<ShopRule> findRuleList(@RequestBody ShopRule ShopRule) throws Exception {
		LOG.info("#findRuleList() 请求参数：{}", gson.toJson(ShopRule));
		List<ShopRule> reList = shopService.findRuleList(ShopRule);
		LOG.info("#findRuleList() 响应参数：{}", gson.toJson(reList));
		return reList;
	}

	// 查看规则库
	@PostMapping(value = "/findDataList")
	public List<ShopRule> findDataList(@RequestBody ShopRule shopRule) throws Exception {
		LOG.info("#findDataList() 请求参数：{}", gson.toJson(shopRule));
		List<ShopRule> reList = shopService.findDataList(shopRule);
		LOG.info("#findDataList() 响应参数：{}", gson.toJson(reList));
		return reList;
	}

	@PostMapping(value = "/datasavePage")
	public int datasavePage(@RequestBody ShopRule shopRule) throws Exception {
		String[] dataarr = shopRule.getDataarr().split(",");
		String[] ruleCode1 = shopRule.getRuleCode().split(",");
		String[] description1 = shopRule.getDescription().split(",");
		String[] type1 = shopRule.getType().split(",");
		String[] section1 = shopRule.getSection().split(",");
		String[] store1 = shopRule.getRuleStore().split(",");
		int flag = 0;
		for (int i = 0; i < dataarr.length; i++) {
			String dataId = dataarr[i];
			String ruleCode = ruleCode1[i];
			String description = description1[i];
			String type = type1[i];
			String section = section1[i];
			String store = store1[i];
			String uuid = UUID.randomUUID().toString().replaceAll("-", "");
			Date date = new Date();
			Timestamp nowdate = new Timestamp(date.getTime());
			ShopRule shopRule2 = new ShopRule();
			shopRule2.setId(uuid);
			shopRule2.setDel(0);
			shopRule2.setCreateTime(nowdate);
			shopRule2.setUpdateTime(nowdate);
			shopRule2.setUpdateUser("admin");
			shopRule2.setShopId(shopRule.getShopId());
			shopRule2.setDataId(dataId);
			shopRule2.setRuleCode(ruleCode);
			shopRule2.setDescription(description);
			shopRule2.setType(type);
			shopRule2.setSection(section);
			shopRule2.setStore(Integer.parseInt(store));
			shopService.datasave(shopRule2);
		}
		return flag;
	}

	// 删除单条
	@RequestMapping(value = "/deletePage", method = RequestMethod.POST)
	public int deletePage(@RequestBody ShopRule shopRule) throws Exception {
		int rul = shopService.delete1(shopRule);
		// LOG.info("#findOne() 响应参数：{}", gson.toJson(ruleData));
		return rul;
	}

	// 产品价格配置查询
	@PostMapping(value = "/searchPrice")
	public List<Shop> searchPrice(@RequestBody Shop shop) throws Exception {
		LOG.info("#findDataList() 请求参数：{}", gson.toJson(shop));
		List<Shop> reList = shopService.searchPrice(shop);
		LOG.info("#findDataList() 响应参数：{}", gson.toJson(reList));
		return reList;
	}

	// 查询shopId
	@PostMapping(value = "/searchShopId")
	public List<Shop> searchShopId(@RequestBody Shop shop) throws Exception {
		LOG.info("#findDataList() 请求参数：{}", gson.toJson(shop));
		List<Shop> reList = shopService.searchShopId(shop);
		LOG.info("#findDataList() 响应参数：{}", gson.toJson(reList));
		return reList;
	}

	// 修改产品价格
	@PostMapping(value = "/updateAllocation")
	public int updateAllocation(@RequestBody Shop shop) throws Exception {
		List<Shop> list = shopService.searchShopId(shop);
		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i).getPrice() + "======================");
			Shop shop2 = new Shop();
			shop2.setId(list.get(i).getId());
			if (i == 0) {
				shop2.setPrice(shop.getFqz());
				int rul = shopService.updateAllocation(shop2);
				return rul;
			}
			if (i == 1) {
				shop2.setPrice(shop.getPfk());
				int rul = shopService.updateAllocation(shop2);
				return rul;
			}
			if (i == 2) {
				shop2.setPrice(shop.getChsi());
				int rul = shopService.updateAllocation(shop2);
				return rul;
			}
			if (i == 3) {
				shop2.setPrice(shop.getGjj());
				int rul = shopService.updateAllocation(shop2);
				return rul;
			}
			if (i == 4) {
				shop2.setPrice(shop.getSheb());
				int rul = shopService.updateAllocation(shop2);
				return rul;
			}
			if (i == 5) {
				shop2.setPrice(shop.getYys());
				int rul = shopService.updateAllocation(shop2);
				return rul;
			}
			if (i == 6) {
				shop2.setPrice(shop.getBank());
				int rul = shopService.updateAllocation(shop2);
				return rul;
			}
		}
		LOG.info("#updateAllocation() 响应参数：{}", gson.toJson(shop));
		return 1;
	}
	
	@PostMapping(value="/findStore")
	public ShopRule findStore(@RequestBody ShopRule shopRule) throws Exception{
		ShopRule shopRule2 = shopService.findStore(shopRule.getId());
		return shopRule2;
	}
	
	@PostMapping(value="/updateStore")
	public int updateStore(@RequestBody ShopRule shopRule) throws Exception{
		int flag = shopService.updateStore(shopRule);
		return flag;
		
	}
}
